package com.ztr.crud_demo.controller;

import com.ztr.crud_demo.entity.UserEntity;
import com.ztr.crud_demo.req.UserReq;
import com.ztr.crud_demo.req.UserSaveReq;
import com.ztr.crud_demo.resp.CommonResp;
import com.ztr.crud_demo.resp.PageResp;
import com.ztr.crud_demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {
    
    @Resource
    private UserService userService;
    
    @GetMapping("/getList")
    public CommonResp getList(UserReq userReq) {
        CommonResp<PageResp<UserEntity>> resp = new CommonResp< >();
        PageResp<UserEntity> list = userService.getList(userReq);
        resp.setContent(list);
        return resp;
    }
    
    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp<UserEntity> resp = new CommonResp<>();
        userService.delete(id);
        return resp;
    }
    // 新增和修改公用方法
    @PostMapping("/save")
    public CommonResp save(@RequestBody UserSaveReq userSaveReq) {
        CommonResp<UserEntity> resp = new CommonResp<>();
        userService.save(userSaveReq);
        return resp;
    }
}
